# -*- coding: utf-8 -*-

from resource_management import *
from resource_management.core.resources.system import Execute
from resource_management.core.source import *
from resource_management.core.resources.system import Directory,File
from resource_management.core.logger import Logger

def kibana_env():
    print("Kibana设置开始")
    import params

    # 删除pid目录
    Directory([params.kibana_pid_dir],action="delete")

    # 创建pid目录
    Directory([params.kibana_log_dir,params.kibana_pid_dir],
              mode=0755,
              cd_access='a',
              owner=params.kibana_user,
              group=params.kibana_group,
              create_parents=True
              )

    # Ensure all files owned by kibana user
    cmd = format("chown -R {kibana_user}:{kibana_group} {kibana_home} {kibana_log_dir} {kibana_pid_dir}")
    Execute(cmd)

    kibana_yml = InlineTemplate(params.kibana_yml)

    File(format("{kibana_conf_dir}/kibana.yml"),
         owner=params.kibana_user,
         group=params.kibana_group,
         content=kibana_yml
         )

    Logger.info("Kibana 设置结束")